package cn.itsource.pethome.quartz.job;

import cn.itsource.pethome.order.domain.ProductOrder;
import cn.itsource.pethome.order.mapper.ProductOrderMapper;
import cn.itsource.pethome.pay.constant.PayConstants;
import cn.itsource.pethome.pay.domain.PayBill;
import cn.itsource.pethome.pay.mapper.PayBillMapper;
import cn.itsource.pethome.quartz.domain.QuartzJobInfo;
import org.quartz.Job;
import org.quartz.JobDataMap;
import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.Date;
import java.util.Map;

/**
 * @description: 描述:
 * @date: 2020/8/28 15:29
 * @author: summer
 * version: 1.0
 */
public class QuartzJob implements Job {

    @Autowired
    private ProductOrderMapper productOrderMapper;

    @Autowired
    private PayBillMapper payBillMapper;

    @Override
    public void execute(JobExecutionContext jobExecutionContext) throws JobExecutionException {
        JobDataMap jobDataMap = jobExecutionContext.getMergedJobDataMap();
        Object params = jobDataMap.get("params");
        QuartzJobInfo jobInfo = (QuartzJobInfo) params;
        switch (jobInfo.getType()){
            case PayConstants.BUSINESSTYPE_PRODUCT:
                Map<String, Object> map = jobInfo.getParams();
                String orderSn = (String) map.get("orderSn");
                ProductOrder productOrder = productOrderMapper.loadByOrderSn(orderSn);
                productOrder.setState(-1);
                productOrderMapper.update(productOrder);

                PayBill payBill = payBillMapper.loadByOrderSn(orderSn);
                payBill.setState(-1);
                payBill.setUpdateTime(new Date());
                payBillMapper.update(payBill);
                break;
        }
    }
}
